package com.tanglon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanglon.constants.SystemConstants;
import com.tanglon.domain.ResponseResult;
import com.tanglon.domain.dto.AddCommentDTO;
import com.tanglon.domain.entity.Comment;
import com.tanglon.domain.vo.CommentVo;
import com.tanglon.domain.vo.PageVo;
import com.tanglon.mapper.CommentMapper;
import com.tanglon.service.CommentService;
import com.tanglon.service.UserService;
import com.tanglon.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.tanglon.constants.SystemConstants.COMMENT_ROOT_ID;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2025-08-09 15:28:17
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        //查询该文章对应的根评论
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId, articleId);

        //查询文章根评论rootId：-1
        wrapper.eq(Comment::getRootId,COMMENT_ROOT_ID);
        //评论类型
        wrapper.eq(Comment::getType, commentType);

        //分页查询
        Page<Comment> page = new Page<>(pageNum, pageSize);
        page(page, wrapper);

        List<CommentVo> commentVoList = toCommentVoList(page.getRecords());

        //查询所有根评论对应的子评论集合
        //查询所有根评论对应的子评论集合，并且赋值给对应的属性
        for (CommentVo commentVo : commentVoList) {
            //查询对应的子评论
            List<CommentVo> children = getChildren(commentVo.getId());
            //赋值
            commentVo.setChildren(children);
        }

        return ResponseResult.okResult(new PageVo(commentVoList,page.getTotal()));
    }

    /**
     * 获得该评论下的子评论
     */
    private List<CommentVo> getChildren(Long id) {
        //根据传来的根评论id查询子评论
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getRootId, id);
        //根据时间排序
        wrapper.orderByAsc(Comment::getCreateTime);

        List<Comment> list = list(wrapper);
        List<CommentVo> commentVoList = toCommentVoList(list);
        return commentVoList;
    }

    /**
     * 该方法遍历集合赋值相关属性
     * @param list
     * @return
     */
    private List<CommentVo> toCommentVoList(List<Comment> list){
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        for(CommentVo commentVo : commentVos){

            // 1. 处理评论作者的昵称和头像
            Long createBy = commentVo.getCreateBy();
            if (createBy != null) {
                //通过creatyBy查询用户的昵称并赋值
                String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
                commentVo.setUsername(nickName);

                //设置该评论用户头像
                String avatar=userService.getById(commentVo.getCreateBy()).getAvatar();
                commentVo.setAvatar(avatar);
            }else {
                // 若 createBy 本身为 null，直接设默认值
                commentVo.setUsername("未知用户");

            }


            //通过toCommentUserId查询用户的昵称并赋值
            //如果toCommentUserId不为-1才查询
            if(commentVo.getToCommentUserId()!=-1){
                String toCommentUserNickName1=userService.getById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentUserName(toCommentUserNickName1);
            }

        }
        return commentVos;
    }

    /**
     * 添加评论
     * @param addCommentDTO
     */
    @Override
    public ResponseResult addComment(AddCommentDTO addCommentDTO) {
        Comment comment = BeanCopyUtils.copyBean(addCommentDTO, Comment.class);
        save(comment);
        return ResponseResult.okResult();
    }


}

